package com.maxd.respository.lucky;

import com.maxd.model.LuckyQueueRecord;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.Repository;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

public interface LuckyQueueRecordRepository extends Repository<LuckyQueueRecord, Long> {

    @Modifying(clearAutomatically = true)
    @Query(value = "update lucky_queue_record set state=:state,audit_content=:auditContent where id=:id",nativeQuery = true)
    int updateLuckyQueueRecord(@Param("id") Long id,@Param("state") Integer state,@Param("auditContent") String auditContent);

    @Query(value = "select * from lucky_queue_record where id=:id",nativeQuery = true)
    LuckyQueueRecord selectById(@Param("id")Long id);

    @Query(value = "select * from lucky_queue_record where user_id=:userId and state=0",nativeQuery = true)
    LuckyQueueRecord selectByUserId(@Param("userId")Long userId);

    @Query(value = "SELECT q.`name` ,r.create_time as createTime,u.nick_name as nickName," +
            "u.image_url as imageUrl,u.id as userId,r.id,r.state,r.audit_content as auditContent FROM " +
            "lucky_queue_record r " +
            "LEFT JOIN lucky_queue q ON r.lucky_queue_id = q.id " +
            "LEFT JOIN user_info u ON u.id = r.user_id ",
            countQuery = "SELECT count(*) FROM " +
                    "lucky_queue_record r " +
                    "LEFT JOIN lucky_queue q ON r.lucky_queue_id = q.id " +
                    "LEFT JOIN user_info u ON u.id = r.user_id " ,
            nativeQuery = true)
    Page<Map<String,Object>> selectLuckyQueueRecordList(Pageable pageable);


    @Query(value = "SELECT q.`name` ,r.create_time as createTime,r.state,r.audit_content as auditContent FROM " +
            "lucky_queue_record r " +
            "LEFT JOIN lucky_queue q ON r.lucky_queue_id = q.id " +
            "where r.user_id=:userId " +
            "ORDER BY r.create_time desc ",
            countQuery = "SELECT count(*) FROM " +
                    "lucky_queue_record r " +
                    "LEFT JOIN lucky_queue q ON r.lucky_queue_id = q.id " +
                    "where r.user_id=:userId ",
            nativeQuery = true)
    Page<Map<String,Object>> selectLuckyQueueRecordByUserId(Pageable pageable,@Param("userId") Long userId);




}
